﻿@page
@model ApisModel
@{
    ViewContext.AddLibraries(ImportLibrary.Highlight);
    ViewBag.Title = Model.Application!.Name + "的API列表";
    ViewBag.Current = "open.apps";
    var tab = true;
    var pane = true;
}

<div class="card data-list">
    <div class="card-header">
        <h2>
            关联@(Model.Application.Name)的API
        </h2>
        <ul class="nav nav-tabs" role="tablist">
            @foreach (var groupName in Model.Document!.Keys)
            {
                <li .class-active="tab" class="nav-item">
                    <a .class-active="tab" class="nav-link" data-toggle="tab" href="#@(groupName)" role="tab">@Model.GetOrAddNamedString("api." + groupName)</a>
                </li>
                tab = false;
            }
        </ul>
    </div>
    <div class="card-body tab-content">
        @foreach (var doc in Model.Document)
        {
            <div .class-show="pane" .class-active="pane" class="tab-pane fade" id="@doc.Key">
                <table class="table table-hover data-all">
                    <thead>
                        <tr>
                            <th class="checkbox"><gt:checkall /></th>
                            <th>API</th>
                            <th>方法</th>
                            <th>参数</th>
                            <th>返回结果</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var api in doc.Value)
                        {
                            var service = Model.ServiceManager.GetOrCreate(api.HttpMethod!, api.RouteTemplate!, () => new OpenService
                            {
                                Category = api.GroupName!,
                                Description = api.Summary?.Summary!,
                                HttpMethod = api.HttpMethod!,
                                Route = api.RouteTemplate!
                            });
                            <tr class="data-item">
                                <td class="checkbox"><gt:checkbox checked="Model.Services!.Contains(service.Id)" value="@service.Id" /></td>
                                <td>
                                    <a _click="modal" asp-page="./Services/Test" asp-route-method="@api.HttpMethod" asp-route-id="@api.RouteTemplate" class="d-block service-route">
                                        <i title="登录后才能够测试" .visible="@(!api.IsAnonymous)" class="bi-lock text-danger"></i>
                                        <i title="已经登录" .visible="@(!api.IsAnonymous)" class="bi-unlock text-success hide"></i>
                                        <strong>@api.RouteTemplate</strong>
                                    </a>
                                    <small class="text-muted">@api.Summary?.Summary</small>
                                </td>
                                <td style="color:@api.HttpMethod!.GetColor()">@api.HttpMethod</td>
                                <td>
                                    <ul>
                                        @foreach (var parameter in api.Parameters!)
                                        {
                                            <li>
                                                @parameter.Name <span class="text-danger" .visible="@parameter.IsRequired">*</span>: {
                                                @if (parameter.Type.IsLinkable())
                                                {
                                                    <a asp-page="./Services/Type" asp-route-type="@parameter.Type.FullName">@parameter.Type.GetRealTypeName()</a>
                                                }
                                                else
                                                {
                                                    <span>@parameter.Type.GetRealTypeName()</span>
                                                }
                                                } @api.GetParameterDescriptor(parameter.Name)?.Summary
                                            </li>
                                        }
                                    </ul>
                                </td>
                                <td>
                                    @api.Summary?.Returns?.Summary
                                    <a class="text-secondary" _click="modal" asp-page="./Services/Result" asp-route-method="@api.HttpMethod" asp-route-route="@api.RouteTemplate" title="查看默认结果" .icon="bi-reply-all"></a>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
            pane = false;
        }
        <a _click="checked" class="btn btn-primary" asp-route-appid="@Model.Application.Id" asp-page-handler="Add" .icon="bi-link"> 关联</a>
    </div>
</div>

@section scripts
{
<script src="~/js/openservice.min.js" .dev-src="~/js/openservice.js"></script>
}
